<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;

class OperateController extends CommonController {
	protected $admin = 'oo_admin';
	protected $tb_xianshi = 'p_xianshi';//限时折扣活动表
	protected $tb_store = 'store';//店铺表
	protected $tb_xianshi_st = 'p_xianshi_store';//店铺参与的活动表
	
	/**
	*限时折扣活动列表
	*
	*/		
    public function index(){

		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}
			
		$num = 15;
		$order = 'xianshi_id desc';
		unset($str);
		unset($data);
		$str = "xianshi_id>%d";
		$data = array(0);	
		//分页
		$show = Page($this->tb_xianshi,$str,$data,$num);		
		
		//查询
		$list = _getList($this->tb_xianshi,$str,$data,'',$order,$_GET['p'],$num) ;

		if (I('get.xianshi_id') || I('get.type')=='add') {
			$this->xianshi_add() ;
		} else if (I('get.type')=='xianshi_store') {
			
			$this->xianshi_store() ;
			
		} else {
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('list',$list);		
			$this->display();			
		}   
    }
	
	/**
	*申请参加活动店铺
	*
	*/		
    public function xianshi_store(){

		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}
			
		$num = 15;

		$order = 'xianshi_id desc';
		unset($str);
		unset($data);
		$str = "xianshi_id>%d";
		$data = array(0);		
		$join['join1'] = ' INNER JOIN __P_XIANSHI_STORE__ as stxs ON xianshi_id=stxs.xs_id ' ;
		$field = '*,stxs.*' ;	
		//分页
		$show = Page($this->tb_xianshi,$str,$data,$num,$join) ;		
		
		//查询
		$list = _getJoinList($this->tb_xianshi,$join,$str,$data,'',$order,$_GET['p'],$num,$field);	
		
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);
		$this->display('xianshi_store');		
    }
	
	/**
	*限时折扣编辑/添加
	*
	*/		
    public function xianshi_add(){

		if (IS_POST) {
			//添加
			unset($data);
			I('post.xianshi_name') ? $data ['xianshi_name'] = I('post.xianshi_name') :$this->error('活动名称不能为空','index') ;
			I('post.xianshi_title') && $data ['xianshi_title'] = I('post.xianshi_title') ;
			I('post.xianshi_explain') && $data ['xianshi_explain'] = I('post.xianshi_explain')  ;
			I('post.start_time') ? $data ['start_time'] = strtotime(I('post.start_time')) :$this->error('开始时间不能为空','index') ;
			
			I('post.end_time') ? $data ['end_time'] = strtotime(I('post.end_time')) :$this->error('结束时间不能为空','index') ;
			
			if ($data ['end_time'] < $data ['start_time']) {
				$this->error('结束时间必须大于开始时间!','index') ;
			}
			
			$pic = upload();
			$pic && $data ['xianshi_pic'] = $pic ;

			I('post.lower_limit') ? $data ['lower_limit'] = I('post.lower_limit') : $data ['lower_limit'] = 0 ;
			
			I('post.xianshi_type') ? $data ['xianshi_type'] = I('post.xianshi_type') : $data ['xianshi_type'] = 2 ;

			if (I('post.xianshi_type')=='1') {
				unset($str) ;
				unset($arr) ;
				$str = "xianshi_id>%d and xianshi_type=%d";
				$arr = array(0,1) ;
				$count = M($this->tb_xianshi)->where($str,$arr)->count();
				if ($count>=1) {
					$this->error('商城首页活动已存在，请选择活动专题页！','xianshi_add')	;
				}
			}
			
			I('post.xianshi_shenhe') ? $data ['xianshi_shenhe'] = I('post.xianshi_shenhe') : $data ['xianshi_shenhe'] = 2 ;
			
			I('post.goods_number') ? $data ['goods_number'] = I('post.goods_number') : $data ['goods_number'] = 0 ;
			//dump($data);die;
			if (I('post.xianshi_id')) {
							
				unset($str) ;
				unset($arr) ;
				$str = "xianshi_id=%d";
				$arr = array(I('post.xianshi_id')) ;
				$gcOne = _getOne($this->tb_xianshi,$str,$arr);
				
				if ($gcOne['state']=='0') {
					$this->error('非法操作！','xianshi_add')	;
				}
				$edit =  _getEdit($this->tb_xianshi,$str,$arr,$data);
				if ($edit) {
					$this->success('修改成功！','index')	;
				} else {
					$this->error('修改失败！','index')	;
				}
			} else {
				
				$add = _getAdd($this->tb_xianshi,$data);
				if ($add) {
					$this->success('添加成功！','index')	;
				} else {
					$this->error('添加失败！','index')	;
				}				
			}

		} else if (I('get.xianshi_id') && I('get.type')=='edit') {
			//修改页面
			unset($str);
			unset($data);
			$str = "xianshi_id=%d";
			$data = array(I('get.xianshi_id'));		
			
			//查询
			$xianshi = _getOne($this->tb_xianshi,$str,$data) ;
			if ($xianshi) {
				//dump($xianshi);die;
				$this->assign('type','edit') ;				
				$this->assign('xianshi',$xianshi) ;
				$this->display('xianshi_add');					
			} else {
				$this->error('页面不存在','index');
			}
		
			
		}  else if (I('get.type')=='add') {
			//新增页面
			$this->assign('type','add') ;				
			$this->display('xianshi_add');	
		} else {
			$this->display();	
		}
	}
	

		
}